/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hotel.jpa;

import entities.GuestUser;
import java.util.Date;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.EntityTransaction;
import javax.persistence.Query;
import javax.transaction.UserTransaction;

/**
 *
 * @author Anton Karlsson
 */
public class UserJpaCtrl implements IJpaCtrl<GuestUser> {

    private final UserTransaction utx;
    private final EntityManagerFactory emf;
    
     public UserJpaCtrl(UserTransaction utx, EntityManagerFactory emf) {
        this.utx = utx;
        this.emf = emf;
     }
    
    @Override
    public void create(GuestUser t) {
       EntityManager em = emf.createEntityManager();
       EntityTransaction tx = em.getTransaction();
       
       tx.begin();
       em.persist(t);
       tx.commit();

       em.close();
    }

    @Override
    public void destroy(GuestUser t) {
         EntityManager em = emf.createEntityManager();
        //We need to find right Product 
        String query = "select p from GuestUser p";
        Query q = em.createQuery(query);
        List<GuestUser> result = q.getResultList();
        GuestUser a = null;
        for(GuestUser u : result){
            if(u.getName().compareTo(t.getName()) == 0){
                a = u;
            }
        }
        
        //Have right user, now we only have to remove it
        EntityTransaction tx = em.getTransaction();
        tx.begin();
        em.remove(a);
        tx.commit();

        em.close(); 
    }

    @Override
    public void edit(GuestUser t) {
        EntityManager em = emf.createEntityManager();
        
        //We need to find right Product to edit
        String query = "select p from GuestUser p";
        Query q = em.createQuery(query);
        List<GuestUser> result = q.getResultList();
        
        GuestUser theProd = null;
        
        for(GuestUser p : result){
            if(p.getId().compareTo(t.getId()) == 0){
                theProd = p;
            }
        }
        
        theProd.setName(t.getName());
        theProd.setPwd(t.getPwd());
        theProd.setAuth(t.getAuth());
        theProd.setEmail(t.getEmail());
        
        //Have right product, now we only have to merge it
        EntityTransaction tx = em.getTransaction();
        tx.begin();
        em.merge(theProd);
        tx.commit();

        em.close();
    }

    @Override
    public GuestUser findEntity(GuestUser t) throws NonexistentEntityException {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public List<GuestUser> findEntities() {
         EntityManager em = emf.createEntityManager();
        
        //Find all users 
        String query = "select u from GuestUser u";
        Query q = em.createQuery(query);
        List<GuestUser> result = q.getResultList();

        em.close();
        
        return result;
    }

    @Override
    public EntityManager getEntityManager() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public int getEntityCount() {
        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public List<GuestUser> findAvailableRooms(int roomNb) {
        throw new UnsupportedOperationException("Not supported yet.");
    }
}
